package org.dt.common.sequence;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.Validate;
import org.dt.common.sequence.persist.TransactionSeqMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 废弃。经Benchmark测试，其吞吐量较Cas小3-4%
 *
 * @author yujiadong
 * @create: 2018/12/03 10:33
 * @deprecated
 **/
@Slf4j
@Deprecated
public class TransactionDelegator extends BaseDelegator {
    private TransactionSeqMapper mapper;

    public TransactionDelegator(TransactionSeqMapper mapper) {
        super(mapper);
        this.mapper = mapper;
    }

    @Transactional(rollbackFor = Exception.class, timeout = 2)
    @Override
    public Sequence applyForOneStep(String biz, long step) {
        Sequence sequence = Objects.requireNonNull(mapper.getNextSequence(biz, step));
        Validate.isTrue(mapper.updateMaxId(biz, step) > 0);
        return sequence;
    }
}
